package edu.stu.book.book.keeping.ui.screen.home

import android.annotation.SuppressLint
import android.provider.CalendarContract.Colors
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.content.ContextCompat
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.rememberPagerState
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import edu.stu.book.book.keeping.R
import edu.stu.book.book.keeping.data.navList
import edu.stu.book.book.keeping.utils.ColorUtils
import kotlinx.coroutines.launch

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter", "ResourceAsColor")
@OptIn(ExperimentalFoundationApi::class, ExperimentalPagerApi::class)
@Composable
fun HomeScreen(navController: NavHostController) {
    var selectIndex by rememberSaveable { mutableIntStateOf(0) }

    val pageState = rememberPagerState(initialPage = 0)

    val context = LocalContext.current

    val scope = rememberCoroutineScope()

    var visible by remember { mutableStateOf(true) }

    /** 状态栏 */
    val systemUiController = rememberSystemUiController()


    systemUiController.setSystemBarsColor(
        color = Color(0x33002fa7),
        darkIcons = true,
    )

    Surface(
        Modifier
            .fillMaxSize()
            .background(MaterialTheme.colorScheme.background)
    ) {
        Scaffold(bottomBar = {
            AnimatedVisibility(
                visible = visible,
                enter = fadeIn(),
                exit = fadeOut(),
            ) {
                NavigationBar(
                    modifier = Modifier.height(60.dp),
                    containerColor = ColorUtils.getColor(context, R.color.white),
                ) {
                    navList.forEachIndexed { index, nav ->
                        Box(
                            modifier = Modifier
                                .fillMaxHeight()
                                .weight(1f)
                                .clickable(
                                    onClick = {
                                        selectIndex = index
                                        scope.launch {
                                            pageState.scrollToPage(index)
                                        }
                                    },
                                    indication = null,
                                    interactionSource = MutableInteractionSource()
                                ),
                            contentAlignment = Alignment.Center
                        ) {
                            Column(
                                horizontalAlignment = Alignment.CenterHorizontally,
                                verticalArrangement = Arrangement.Bottom
                            ) {
                                Icon(
                                    modifier = Modifier.size(24.dp),
                                    imageVector = nav.icon,
                                    contentDescription = nav.title,
                                    tint = ColorUtils.getColor(context),
                                )
                                Spacer(Modifier.padding(top = 4.dp))
                                AnimatedVisibility(visible = index == selectIndex) {
                                    Text(
                                        text = nav.title,
                                        color = ColorUtils.getColor(context),
                                        fontSize = 14.sp,
                                    )
                                }
                            }
                        }
                    }
                }
            }
        }, content = { innerPadding ->
            Box {
                HorizontalPager(
                    count = navList.size,
                    state = pageState,
                    contentPadding = PaddingValues(horizontal = 0.dp),
                    modifier = Modifier
                        .fillMaxSize(),
                    userScrollEnabled = false,
                ) { page ->
                    when (page) {
                        0 -> AddScreen(navController)
                        1 -> ChartScreen(navController)
                        2 -> IndexScreen(navController)
                        3 -> FindScreen(navController)
                        4 -> UserScreen(navController)
                    }
                }
                LaunchedEffect(pageState) {
                    snapshotFlow { pageState.currentPage }.collect { page ->
                        selectIndex = page
                        visible = true
                    }
                }
            }
        })
    }
}